package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.accesservice.dao.IDaoPersonnelArea;
import com.ocom.accesservice.service.PersonnelAreaService;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.web.PersonnelAreaRequest;
import com.ocom.common.vo.access.PersonnelAreaVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonnelAreaServiceImpl extends ServiceImpl<IDaoPersonnelArea, PersonnelAreaVo> implements PersonnelAreaService {

    @Autowired
    private IDaoPersonnelArea iDaoPersonnelArea;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<PersonnelAreaVo> getAreaAccessList(Long comId, PersonnelAreaRequest personnelAreaRequest) {
        if (personnelAreaRequest.getPage()!=-1 && personnelAreaRequest.getSize()!=-1){
            PageHelper.startPage(personnelAreaRequest.getPage(),personnelAreaRequest.getSize());
        }
        String[] arr=personnelAreaRequest.getDeptIds().split(",");
        List<Integer> list=new ArrayList<>();
        for (String s : arr) {
            list.add(Integer.valueOf(s));
        }
        return iDaoPersonnelArea.getAreaAccessList(personnelAreaRequest.getComId(),personnelAreaRequest.getAreaNo(),
                list,personnelAreaRequest.getPName(),personnelAreaRequest.getPCode(),
                personnelAreaRequest.getAccess());
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<PersonnelAreaVo> getAreaAccessListBu(Long comId, PersonnelAreaRequest personnelAreaRequest) {
        if (personnelAreaRequest.getPage()!=-1 && personnelAreaRequest.getSize()!=-1){
            PageHelper.startPage(personnelAreaRequest.getPage(),personnelAreaRequest.getSize());
        }
        String[] arr=personnelAreaRequest.getDeptIds().split(",");
        List<Integer> list=new ArrayList<>();
        for (String s : arr) {
            list.add(Integer.valueOf(s));
        }
        return iDaoPersonnelArea.getAreaAccessListBu(personnelAreaRequest.getComId(),personnelAreaRequest.getAreaNo(),
                list,personnelAreaRequest.getPName(),personnelAreaRequest.getPCode(),
                personnelAreaRequest.getAccess());
    }
}
